package services

import (
	"encoding/json"
	"gorm.io/gorm"
	"math"
	"mygo/app/models"
	"mygo/app/tools"
	"strconv"
	"strings"
)

var dayMap = map[int]string{
	1: "明天",
	2: "后天",
	3: "大后天",
}

var waveGradeMap = map[string][]float64{
	"无浪": {0, 0},
	"微浪": {0, 0.1},
	"小浪": {0.1, 0.5},
	"轻浪": {0.5, 1.25},
	"中浪": {1.25, 2.5},
	"大浪": {2.5, 4},
	"巨浪": {4, 6},
	"狂浪": {6, 9},
	"狂涛": {9, 14},
	"怒涛": {14, math.MaxFloat64},
}

func getWaveGrade(value interface{}) string {
	switch v := value.(type) {
	case float64:
		for grade, rangeValues := range waveGradeMap {
			if v >= rangeValues[0] && v < rangeValues[1] {
				return grade
			}
		}
	case string:
		if rangeValues, ok := waveGradeMap[v]; ok {
			return strconv.FormatFloat(tools.Average(rangeValues), 'f', 1, 64)
		}
	}
	return "未知"
}

// 根据小时判断早上、中午、傍晚
func getDayTime(time string) string {
	timeArr := strings.Split(time, ":")
	hour, _ := strconv.Atoi(timeArr[0])
	if hour >= 0 && hour < 10 {
		return "早上"
	} else if hour >= 10 && hour < 14 {
		return "中午"
	} else {
		return "傍晚"
	}
}

func HandelRahx(forecast *models.Forecast, time string, db *gorm.DB) {
	timeArr := strings.Split(time, "-")
	year := timeArr[0]
	month := timeArr[1]
	day := timeArr[2]

	//获取上一天的日期
	y, m, d := tools.GetNextDay(year, month, day, -1)
	timeLast := y + "-" + m + "-" + d

	for i, report := range forecast.Reports {
		if i == 0 {
			//获取上一天rahx的数据
			var foreList models.ForeList
			var raData = models.Forecast{}
			if db.Where("code = ?", "rahx").First(&foreList).RowsAffected > 0 {
				var foreDetail models.ForeDetailJson
				db.Where("foreListId = ? AND time = ? AND state != ?", foreList.Id, timeLast, "0").First(&foreDetail)
				json.Unmarshal(foreDetail.Data, &raData)
				if len(raData.Reports) > 0 {
					for j := range report.Data {
						for k := range report.Data[j] {
							report.Data[j][k].Value = raData.Reports[0].Data[j][k].Value
						}
					}
				}
			}

			//for j := 1; j < 4; j++ {
			//	y, m, d := tools.GetNextDay(year, month, day, j-1)
			//	t1 := y + "-" + m + "-" + d
			//	y, m, d = tools.GetNextDay(year, month, day, j)
			//	t2 := y + "-" + m + "-" + d
			//	var waveValueArr []float64
			//	var waveAngleArr []string
			//	var raList []models.RaForeOne
			//	if db.Where("(date = ? AND time >= ?) OR (date > ? AND date < ?) OR (date = ? AND time <= ?)", t1, "20:00", t1, t2, t2, "20:00").Find(&raList).RowsAffected > 0 {
			//		for _, v := range raList {
			//			waveValueArr = append(waveValueArr, tools.StrToFloat64(v.WaveValue))
			//			waveAngle, _ := strconv.ParseFloat(v.WaveAngle, 64)
			//			for k, vRange := range angleMap {
			//				if v.WaveAngle == "" {
			//					break
			//				}
			//				if waveAngle >= vRange[0] && waveAngle < vRange[1] {
			//					waveAngleArr = append(waveAngleArr, k)
			//					break
			//				}
			//				if k == "N'" {
			//					if (waveAngle >= vRange[0] && waveAngle < 360) || (waveAngle >= 0 && waveAngle < vRange[1]) {
			//						waveAngleArr = append(waveAngleArr, k)
			//						break
			//					}
			//				}
			//			}
			//		}
			//		maxValue, minValue, maxIdx, minIdx := tools.MaxAndMinAndIndex(waveValueArr)
			//		maxValue = tools.Float64ToFixed(maxValue, 1)
			//		minValue = tools.Float64ToFixed(minValue, 1)
			//		if maxValue-minValue > 0.3 {
			//			var oneValue, twoValue float64
			//			if minIdx < maxIdx {
			//				bw := getDayTime(tools.IntToStr((maxIdx+20)%24) + ":00")
			//				report.Data[j][4].Value = tools.Float64ToStr(minValue) + "米" + "，" + dayMap[j] + bw + "增大到" + tools.Float64ToStr(maxValue) + "米"
			//				report.Data[j+3][4].Value = tools.Float64ToStr(minValue) + "米" + "，" + dayMap[j] + bw + "增大到" + tools.Float64ToStr(maxValue) + "米"
			//				oneValue = minValue
			//				twoValue = maxValue
			//			} else {
			//				bw := getDayTime(tools.IntToStr((minIdx+20)%24) + ":00")
			//				report.Data[j][4].Value = tools.Float64ToStr(maxValue) + "米" + "，" + dayMap[j] + bw + "减小到" + tools.Float64ToStr(minValue) + "米"
			//				report.Data[j+3][4].Value = tools.Float64ToStr(maxValue) + "米" + "，" + dayMap[j] + bw + "减小到" + tools.Float64ToStr(minValue) + "米"
			//				oneValue = maxValue
			//				twoValue = minValue
			//			}
			//			if getWaveGrade(oneValue) == getWaveGrade(twoValue) {
			//				report.Data[j][5].Value = getWaveGrade(oneValue)
			//				report.Data[j+3][5].Value = getWaveGrade(oneValue)
			//			} else {
			//				report.Data[j][5].Value = getWaveGrade(oneValue) + "到" + getWaveGrade(twoValue)
			//				report.Data[j+3][5].Value = getWaveGrade(oneValue) + "到" + getWaveGrade(twoValue)
			//			}
			//		} else {
			//			report.Data[j][4].Value = tools.Float64ToStr(tools.Float64ToFixed(tools.Average(waveValueArr), 1)) + "米"
			//			report.Data[j+3][4].Value = tools.Float64ToStr(tools.Float64ToFixed(tools.Average(waveValueArr), 1)) + "米"
			//			report.Data[j][5].Value = getWaveGrade(tools.Float64ToFixed(tools.Average(waveValueArr), 1))
			//			report.Data[j+3][5].Value = getWaveGrade(tools.Float64ToFixed(tools.Average(waveValueArr), 1))
			//		}
			//
			//		if waveAngleArr[0] == waveAngleArr[len(waveAngleArr)-1] {
			//			report.Data[j][6].Value = waveAngleArr[0]
			//			report.Data[j+3][6].Value = waveAngleArr[0]
			//		} else {
			//			report.Data[j][6].Value = waveAngleArr[0] + "转" + waveAngleArr[len(waveAngleArr)-1]
			//			report.Data[j+3][6].Value = waveAngleArr[0] + "转" + waveAngleArr[len(waveAngleArr)-1]
			//		}
			//	}
			//}
		}
	}
}

func HandelRayg(forecast *models.Forecast, time string, db *gorm.DB) {
	timeArr := strings.Split(time, "-")
	year := timeArr[0]
	month := timeArr[1]
	day := timeArr[2]
	timeNow := year + "-" + month + "-" + day
	var foreList models.ForeList
	var raData = models.Forecast{}
	if db.Where("code = ?", "rahx").First(&foreList).RowsAffected > 0 {
		var foreDetail models.ForeDetailJson
		db.Where("foreListId = ? AND time = ? AND state != ?", foreList.Id, timeNow, "0").First(&foreDetail)
		json.Unmarshal(foreDetail.Data, &raData)
	}
	for i, report := range forecast.Reports {
		if i == 1 {
			if len(raData.Reports) == 0 {
				continue
			}
			for j := 1; j < 4; j++ {
				waveValues1 := tools.GetNumFloat64(raData.Reports[0].Data[j][4].Value)
				waveValues2 := tools.GetNumFloat64(raData.Reports[0].Data[j+3][4].Value)
				if len(waveValues1) == 0 || len(waveValues2) == 0 {
					continue
				}
				strValue1 := ""
				if strings.Contains(raData.Reports[0].Data[j][4].Value, "到") {
					strValue1 = strings.Split(strings.Split(raData.Reports[0].Data[j][4].Value, "到")[0], "，")[1]
				}
				strValue2 := ""
				if strings.Contains(raData.Reports[0].Data[j+3][4].Value, "到") {
					strValue2 = strings.Split(strings.Split(raData.Reports[0].Data[j+3][4].Value, "到")[0], "，")[1]
				}
				waveValues1 = amendValueArr(waveValues1)
				waveValues2 = amendValueArrRa(waveValues2)
				if len(waveValues1) == 1 {
					report.Data[j][4].Value = tools.Float64ToFixedStr(waveValues1[0], 1) + "米"
					report.Data[j][5].Value = getWaveGrade(tools.Float64ToFixed(waveValues1[0], 1))
				} else {
					report.Data[j][4].Value = tools.Float64ToFixedStr(waveValues1[0], 1) + "米，" + strValue1 + "到" + tools.Float64ToFixedStr(waveValues1[1], 1) + "米"
					if getWaveGrade(waveValues1[0]) == getWaveGrade(waveValues1[1]) {
						report.Data[j][5].Value = getWaveGrade(waveValues1[0])
					} else {
						report.Data[j][5].Value = getWaveGrade(waveValues1[0]) + "到" + getWaveGrade(waveValues1[1])
					}
				}
				if len(waveValues2) == 1 {
					report.Data[j+3][4].Value = tools.Float64ToFixedStr(waveValues2[0], 1) + "米"
					report.Data[j+3][5].Value = getWaveGrade(tools.Float64ToFixed(waveValues2[0], 1))
				} else {
					report.Data[j+3][4].Value = tools.Float64ToFixedStr(waveValues2[0], 1) + "米，" + strValue2 + "到" + tools.Float64ToFixedStr(waveValues2[1], 1) + "米"
					if getWaveGrade(waveValues2[0]) == getWaveGrade(waveValues2[1]) {
						report.Data[j+3][5].Value = getWaveGrade(waveValues2[0])
					} else {
						report.Data[j+3][5].Value = getWaveGrade(waveValues2[0]) + "到" + getWaveGrade(waveValues2[1])
					}
				}
				report.Data[j][6].Value = raData.Reports[0].Data[j][6].Value
				report.Data[j+3][6].Value = "SE"
				report.Data[j][3].Value = raData.Reports[0].Data[j][3].Value
				report.Data[j+3][3].Value = raData.Reports[0].Data[j+3][3].Value

				row2 := raData.Reports[0].Data[j+3][2].Value
				//str2 := ""
				//获取row2中的整数
				if strings.Contains(row2, "，") && strings.Contains(row2, "到") {
					wind1 := strings.Split(row2, "，")[0]
					wind2 := strings.Split(strings.Split(row2, "，")[1], "到")[1]
					midStr := strings.Split(strings.Split(row2, "，")[1], "到")[0]
					report.Data[j][2].Value = tools.ModifyWindSpeed(wind1, "0.5") + "，" + midStr + "到" + tools.ModifyWindSpeed(wind2, "0.5")
				} else {
					report.Data[j][2].Value = tools.ModifyWindSpeed(row2, "0.5")
				}
				if strings.Contains(row2, "，") && strings.Contains(row2, "到") {
					wind1 := strings.Split(row2, "，")[0]
					wind2 := strings.Split(strings.Split(row2, "，")[1], "到")[1]
					midStr := strings.Split(strings.Split(row2, "，")[1], "到")[0]
					report.Data[j+3][2].Value = tools.ModifyWindSpeed(wind1, "2") + "，" + midStr + "到" + tools.ModifyWindSpeed(wind2, "2")
				} else {
					report.Data[j+3][2].Value = tools.ModifyWindSpeed(row2, "2")
				}
				rowNumArr := tools.GetNum(row2)
				if len(rowNumArr) > 0 {
					if rowNumArr[0] < 4 {
						report.Data[j+3][2].Value = "2-3"
					}
				}
			}
		} else {
			for j := 1; j < 4; j++ {
				y, m, d := tools.GetNextDay(year, month, day, j)
				t1 := y + "-" + m + "-" + d
				var bjTide models.TwcTide
				if db.Where("date = ? AND station = ?", t1, "北麂").First(&bjTide).RowsAffected >= 0 {
					if bjTide.TimeHigh1 != "" {
						tempArr := strings.Split(bjTide.TimeHigh1, ":")
						report.Data[j][2].Value = tempArr[0] + "点" + tempArr[1] + "分"
					}
					if bjTide.TimeHigh2 != "" {
						tempArr := strings.Split(bjTide.TimeHigh2, ":")
						report.Data[j][3].Value = tempArr[0] + "点" + tempArr[1] + "分"
					}
					if bjTide.TimeLow1 != "" {
						tempArr := strings.Split(bjTide.TimeLow1, ":")
						report.Data[j][4].Value = tempArr[0] + "点" + tempArr[1] + "分"
					}
					if bjTide.TimeLow2 != "" {
						tempArr := strings.Split(bjTide.TimeLow2, ":")
						report.Data[j][5].Value = tempArr[0] + "点" + tempArr[1] + "分"
					}
				}
				var raTide models.TwcTide
				if db.Where("date = ? AND station = ?", t1, "瑞安").First(&raTide).RowsAffected >= 0 {
					if raTide.TimeHigh1 != "" {
						tempArr := strings.Split(raTide.TimeHigh1, ":")
						report.Data[j+3][2].Value = tempArr[0] + "点" + tempArr[1] + "分"
					}
					if raTide.TimeHigh2 != "" {
						tempArr := strings.Split(raTide.TimeHigh2, ":")
						report.Data[j+3][3].Value = tempArr[0] + "点" + tempArr[1] + "分"
					}
					if raTide.TimeLow1 != "" {
						tempArr := strings.Split(raTide.TimeLow1, ":")
						report.Data[j+3][4].Value = tempArr[0] + "点" + tempArr[1] + "分"
					}
					if raTide.TimeLow2 != "" {
						tempArr := strings.Split(raTide.TimeLow2, ":")
						report.Data[j+3][5].Value = tempArr[0] + "点" + tempArr[1] + "分"
					}
				}
			}
		}
	}
}

// amend
func amendValue(value float64) float64 {
	if value <= 1.2 {
		return tools.Float64ToFixed(value-0.1, 1)
	} else if value > 1.2 && value <= 1.8 {
		return tools.Float64ToFixed(value-0.2, 1)
	} else if value > 1.8 && value <= 2.4 {
		return tools.Float64ToFixed(value-0.3, 1)
	} else if value > 2.4 && value <= 3.4 {
		return tools.Float64ToFixed(value-0.4, 1)
	} else {
		return tools.Float64ToFixed(value-0.5, 1)
	}
}

func amendValueArr(value []float64) []float64 {
	//	求最大值
	//maxValue := tools.MaxFloat64(value)
	//if maxValue <= 1.2 {
	//	for i, v := range value {
	//		value[i] = tools.Float64ToFixed(v-0.1, 1)
	//	}
	//} else if maxValue > 1.2 && maxValue <= 1.8 {
	//	for i, v := range value {
	//		value[i] = tools.Float64ToFixed(v-0.2, 1)
	//	}
	//} else if maxValue > 1.8 && maxValue <= 2.4 {
	//	for i, v := range value {
	//		value[i] = tools.Float64ToFixed(v-0.3, 1)
	//	}
	//} else if maxValue > 2.4 && maxValue <= 3.4 {
	//	for i, v := range value {
	//		value[i] = tools.Float64ToFixed(v-0.4, 1)
	//	}
	//} else {
	//	for i, v := range value {
	//		value[i] = tools.Float64ToFixed(v-0.5, 1)
	//	}
	//}
	//return value

	for i, v := range value {
		if v <= 1.2 {
			value[i] = tools.Float64ToFixed(v-0.1, 1)
		} else if v > 1.2 && v <= 1.8 {
			value[i] = tools.Float64ToFixed(v-0.2, 1)
		} else if v > 1.8 && v <= 2.4 {
			value[i] = tools.Float64ToFixed(v-0.3, 1)
		} else if v > 2.4 && v <= 3.4 {
			value[i] = tools.Float64ToFixed(v-0.4, 1)
		} else {
			value[i] = tools.Float64ToFixed(v-0.5, 1)
		}
	}
	return value
}

// amendValueArrRa  减小一半并且向上保留一位小数
func amendValueArrRa(value []float64) []float64 {
	for i, v := range value {
		value[i] = tools.Float64ToFixed(v/2, 1)
	}
	return value
}
